package TestCases;

import java.util.Random;

public class CreatSQL {
	private static final Random random = new Random();

	public static void main(String[] args) {
		// 调用方法生成包含300个字段的UPDATE语句，每个字段值是一个随机中文字符
		String updateStatement = generateUpdateStatement1(300);
		System.out.println(updateStatement);
    }
	
	public static String generateUpdateStatement1(int numFields) {
        StringBuilder sb = new StringBuilder("UPDATE zl_zzslb SET ");
        StringBuilder placeholderSection = new StringBuilder("(");

        for (int i = 1; i <= numFields - 1; i++) { // 减2是因为已经有XZ、GPH、JHLH三个字段以及ROWID
//        	char lastRandomChar1 = generateRandomChineseChar();
//        	char lastRandomChar2 = generateRandomChineseChar();
            sb.append("FIELD").append(i).append("=:").append(i).append(", ");
            placeholderSection.append(i).append(" = "+i+",");
        }
       
        // 添加最后两个字段（假设最后一个字段不是ROWID）
        sb.append("FIELD").append(numFields - 1).append("=:").append(numFields-1);
        placeholderSection.append(numFields).append(" = ").append("300) ");

        sb.append(" WHERE ROWID=:").append(numFields).append("; ");
        sb.append(placeholderSection.toString());

        return sb.toString();
    }
	
    // 简化的随机生成汉字方法，实际场景下可能需要更完善的字库
    private static char generateRandomChineseChar() {
        // 假设我们有足够大的汉字Unicode范围
        int min = 0x4E00; // 汉字Unicode起始码
        int max = 0x9FA5; // 汉字Unicode结束码
        return (char) (random.nextInt(max - min + 1) + min);
    }

    public static String generateUpdateStatement(int numFields) {
        StringBuilder sb = new StringBuilder("UPDATE zl_zzslb SET ");
        StringBuilder placeholderSection = new StringBuilder("(1 = '");

        for (int i = 1; i <= numFields - 2; i++) {
            char randomChar = generateRandomChineseChar();
            sb.append("COLUMN").append(i).append("='").append(randomChar).append("' , ");
            placeholderSection.append("'").append(randomChar).append("' = ");
            if (i != numFields - 2) {
                placeholderSection.append(",");
            }
        }

        // 添加最后两个字段
        char lastRandomChar1 = generateRandomChineseChar();
        char lastRandomChar2 = generateRandomChineseChar();
        sb.append("COLUMN").append(numFields - 1).append("='").append(lastRandomChar1).append("' ) ");
        placeholderSection.append("'").append(lastRandomChar2).append("' = ");

        sb.append("WHERE ROWID=:").append(numFields + 1).append("; ");
        // 修正placeholderSection，移除多余的'.append()'并正确添加逗号
        placeholderSection.deleteCharAt(placeholderSection.length() - 2); // 移除最后一个多余的逗号和空格
        sb.append("(").append(placeholderSection.toString()).append(")");

        return sb.toString();
    }
}
